home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / varinc.lzh / REIN.C < prev    next >
Text File  |  1979-11-30  |  2KB  |  37 lines

  1. /*****************************************************************************/
  2. /* REIN reindents C programs, converting from one number of spaces per       */
  3. /*   indent to another. REIN is a filter. The following call converts from 5 */
  4. /*   spaces per indent to 3:                                                 */
  5. /*     REIN 5 3 <OLDPGM.C >NEWPGM.C                                          */
  6. /*****************************************************************************/
  7.  
  8. #include <stdio.h>
  9.  
  10. main(ac, av)                             /* main() is passed two parameters. */
  11. unsigned ac;               /* command line argument count; 1 if no arguments */
  12. char *av[];                /* array of pointers to strings from command line */
  13.  
  14.    {
  15.    char buf[BUFSIZ];
  16.    short old_indent, new_indent, indent, new_spc;
  17.  
  18.    if (ac != 3)
  19.       err_exit("Usage: reindent #_old_indent #_new_indent\n", "");
  20.  
  21.    old_indent = atoi(av[1]);                 /* Convert command-line numbers */
  22.    new_indent = atoi(av[2]);                 /*   from string to binary.     */
  23.    if (old_indent < 1 || new_indent < 1)
  24.       err_exit("Reindent: pass two numbers, both > 0", "");
  25.    while (gets(buf))                            /* Loop for each input line. */
  26.       {
  27.  
  28.       /* Count leading spaces. */
  29.       for (indent = 0; buf[indent] == ' '; ++indent)
  30.          ;
  31.       new_spc = indent * new_indent / old_indent;
  32.       while (new_spc--)                         /* Print new leading spaces. */
  33.          putchar(' ');
  34.       puts(&buf[indent]);                /* Print rest of line after spaces. */
  35.       }
  36.    }
  37.